from scipy.stats import norm
import pandas as pd

data = pd.read_csv(
     r'C:\Users\Administrator\Downloads\模块二作业 (1)\模块二作业\ab_data.csv',
 )
# print(data.head(3))

# 样本量
old_page_cond = data['landing_page']=="old_page"
new_page_cond = data['landing_page']=="new_page"
old_page_click_count = data[old_page_cond].count().user_id
new_page_click_count = data[new_page_cond].count().user_id
# print(old_page_click_count)
# print(new_page_click_count)
# 平均値
old_mean = data.groupby(["landing_page"]).mean()['converted']['old_page'].round(4)
new_mean = data.groupby(["landing_page"]).mean()['converted']['new_page'].round(4)
# 标准差
old_std = data.groupby(["landing_page"]).std()['converted']['old_page'].round(4)
new_std = data.groupby(["landing_page"]).std()['converted']['new_page'].round(4)
# print(old_std)
# print(new_std)



class ABtest_u():
    '''
    双样本双尾均值检验
    '''

    def __init__(self, x1: float, x2: float, s1: float, s2: float, n1: int, n2: int, a: float = 0.05, b: float = 0.2):
        self.x1 = x1  # 对照组均值
        self.x2 = x2  # 测试组均值
        self.s1 = s1  # 对照组标准差
        self.s2 = s2  # 测试组标准差
        self.n1 = n1  # 对照组样本量
        self.n2 = n2  # 测试组样本量
        self.a = a  # alpha
        self.b = b  # beta

    def significance_u(self) -> (int, float, float):
        '''
        双样本双尾均值显著性检验
        '''
        z = (self.x1 - self.x2) / pow(self.s1 ** 2 / self.n1 + self.s2 ** 2 / self.n2, 1 / 2)
        if z > 0:
            p = (1 - norm.cdf(z)) * 2
            if p < self.a:  # 拒绝原假设，接受备选假设
                f = 1
            else:  # 接受原假设
                f = 0
        else:
            p = 2 * norm.cdf(z)
            if p < self.a:  # 拒绝原假设，接受备选假设
                f = 1
            else:  # 接受原假设
                f = 0
        return f, format(z, '.2f'), format(p, '.2f')

    def confidence_u(self) -> tuple:
        '''
        双样本均值置信区间
        '''
        d = norm.ppf(1 - self.a / 2) * pow(self.s1 ** 2 / self.n1 + self.s2 ** 2 / self.n2, 1 / 2)
        floor = -(self.x1 - self.x2 - d)
        ceil = -(self.x1 - self.x2 + d)
        return (format(floor, '.2f'), format(ceil, '.2f'))

    def power_u(self) -> float:
        '''
        双样本均数功效
        '''
        z = abs(self.x1 - self.x2) / pow(self.s1 ** 2 / self.n1 + self.s2 ** 2 / self.n2, 1 / 2) - norm.ppf(
            1 - self.a / 2)
        b = 1 - norm.cdf(z)
        power = 1 - b
        return power

    def main(self):
        f, z, p = self.significance_u()
        ci = self.confidence_u()
        power = self.power_u()
        print('保留组均值：%s'%(self.x1))
        print('测试组均值：%s'%(self.x2))
        print('是否显著：' + ('统计效果显著，拒绝原假设' if f == 1 else '统计效果不显著，不能拒绝原假设'))
        print('变化度：' + format((self.x2 - self.x1) / self.x1, '.2%'))
        print('置信区间：',ci)
        print('p-value：%s'%p)
        print('功效：%s'%power)
test = ABtest_u(x1=new_mean, x2=old_mean, s1=new_std, s2=old_std, n1=new_page_click_count, n2=old_page_click_count)
test.main()
"""
H0：用户不选择新版本
H1: 用户选择新版本
结论：
进入bannerA进行转化购买的是0.1188
进入bannerB进行转化购买的是0.1205
p值0.16（a=5%）,统计效果不显著，不能拒绝原假设
"""
